<template>
  <div class="bg">
    <el-card class="box-card">
      <el-row type="flex" class="row-bg" justify="end">
        <!-- <el-col :span="4" :offset="24">
          <el-input style="width:300px" placeholder="请输入商家名称/联系人/手机号码查询"></el-input>
        </el-col>-->
        <el-col :span="6" :offset="16">
          <el-select v-model="status" placeholder="请选择话题状态" style="width:200px">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-col>
        <el-col :span="6" :offset="1">
          <el-input style="width:300px" v-model="selectCode" placeholder="请输入话题关键词"></el-input>
        </el-col>

        <el-col :span="6" :offset="1">
          <div style="display:flex">
            <el-button type="primary" @click="searchlook">查询</el-button>
            <el-button type="success" @click="Reset">重置</el-button>
          </div>
        </el-col>
      </el-row>
    </el-card>

    <!-- 表格展示 -->
    <el-card class="card">
      <div slot="header">
        <el-row type="flex" justify="end">
          <!-- <el-col :span="20">
          </el-col>-->
          <el-col :span="2">
            <el-button type="success" @click="classify">话题分类</el-button>
          </el-col>
          <el-col :span="2">
            <el-button type="success" @click="addBialogVisible">创建话题</el-button>
          </el-col>
        </el-row>
      </div>
      <!-- 表格展示 -->
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="index" width="100" label="序号" align="center"></el-table-column>
        <el-table-column prop="topicName" label="话题名称" align="center"></el-table-column>
        <!-- <el-table-column prop="createName" label="话题创建" align="center"></el-table-column> -->
        <el-table-column prop="topicTypeName" label="话题类型" align="center">
          <!-- <template slot-scope="scope">
            <span>{{scope.row.topicStatus==1?'待完善':scope.row.topicStatus==2?'正常':scope.row.topicStatus==3?'热门':'火爆'}}</span>
          </template>-->
        </el-table-column>
        <el-table-column prop="topicPostNumber" label="讨论" align="center"></el-table-column>
        <el-table-column prop="topicViewNumber" label="阅读" align="center"></el-table-column>
        <el-table-column prop="createName" label="创建者" align="center"></el-table-column>
        <el-table-column prop="topicCreateTime" label="创建时间" align="center"></el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <span>{{ topicStatus(scope.row.topicStatus)}}</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="220" align="center">
          <template slot-scope="scope">
            <el-button
              @click="handleClick(scope.row)"
              type="primary"
              size="small"
            >{{scope.row.topicSort==1?'置顶':'取消'}}</el-button>
            <el-button @click="compile(scope.row)" type="warning" size="small">编辑</el-button>
            <el-button @click="themeDelet(scope.row)" type="danger" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        layout="total,  prev, pager, next, jumper"
        :total="total"
        :current-page="current"
        @current-change="handleCurrentChange"
        style="margin-top:24px"
      ></el-pagination>
    </el-card>

    <!-- 创建话题 -->
    <el-dialog
      :title="title==1?'创建话题':'编辑话题'"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
      center
    >
      <createTopic
        @ruleForm="ruleForm"
        :topicId="topicId"
        ref="mychild"
        :allTheme="allTheme"
        @close="close"
        :typeShow="typeShow"
      />
    </el-dialog>
  </div>
</template>

<script>
import { export2Excel } from "@/components/commom.js";
import createTopic from "@/components/createTopic";
export default {
  components: {
    createTopic
  },
  data() {
    return {
      //获取所有话题类型
      allTheme: [],
      // 1待完善 2 正常 3 热门 4 火爆
      options: [
        {
          value: 1,
          label: "待完善"
        },
        {
          value: 2,
          label: "正常"
        },
        {
          value: 3,
          label: "热门"
        },
        {
          value: 4,
          label: "火爆"
        },
        {
          value: 5,
          label: "待生成"
        }
      ],
      selectCode: "",
      status: "",
      dialogVisible: false,
      downloadLoading: false,
      current: 1, //分页数据
      tableData: [],
      total: 0,
      topicId: 0,
      title: "", //话题名称
      typeShow: true //默认状态
    };
  },
  mounted() {
    this.topiclist();
    // 获取所有的话题类型
    this.themetype();
  },
  methods: {
    // 获取话题
    topiclist() {
      let param = {
        size: 10,
        current: this.current,
        selectCode: this.selectCode,
        status: this.status
      };
      this.$http.article.topiclist(param).then(res => {
        this.tableData = res.result.records;
        this.total = res.result.total;
        console.log("获取话题列表", res);
        //显示每页展示的数量
        res.result.records.forEach((item, index) => {
          //res.data.data.list 表格数据     forEach为每一项添加数据
          item.index = index + 1 + (this.current - 1) * 10;
          // this.currentPage当前页  this.pageParams.size 每页显示多少    :page-sizes="[10,20,30,50]" 分页大小

          return item;
        });
      });
    },
    // 话题状态展示
    topicStatus(val) {
      switch (val) {
        case 1:
          return "待完善";
          break;
        case 2:
          return "正常";
          break;
        case 3:
          return "热门";
          break;
        case 4:
          return "火爆";
          break;
        case 5:
          return "待生成";
          break;
      }
    },
    // 关闭类型
    close(val) {
      console.log("关闭类型", val);
    },
    // 获取所有话题类别
    themetype() {
      this.$http.article.getThemeType().then(res => {
        this.allTheme = res.result;
        console.log("获取所有话题类型", res);
      });
    },
    // 删除话题列表
    themeDelet(row) {
      console.log("删除话题列表", row);
      this.$confirm(`是否确认删除？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let params = {
            pet_topicId: row.topicId
          };
          console.log(params, "params");
          this.$http.article.deletetopic(params).then(res => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg
              });
              this.topiclist();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    // 创建话题
    ruleForm(val) {
      this.$http.article.addtopic(val).then(res => {
        console.log("创建获取话题", res);
        if (res.code == 200) {
          this.dialogVisible = false;
          this.topiclist();
        }
      });
    },
    //话题编辑
    compile(val) {
      this.typeShow = false;
      this.title = 2;
      this.$http.article.topicGet({ pet_topicId: val.topicId }).then(res => {
        if (res.result == null || "") {
          return  
        } else {
          // 调用子组件方法
          this.$refs.mychild.fetchData(res.result);
          this.$refs.mychild.resetForm("ruleForm");
          console.log("获取详情", res);
        }
      });
      this.dialogVisible = true;
    },
    //话题置顶
    handleClick(val) {
      console.log("置顶", val);
      let sortMessage = "";
      if (val.topicSort == 1) {
        sortMessage = "置顶";
      } else {
        sortMessage = "取消置顶";
      }
      this.$confirm(`此操作将${sortMessage}, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let params = {
            topicId: val.topicId,
            topicSort: val.topicSort == 1 ? 9999 : 1
          };
          console.log(params, "params");
          this.$http.article.addtopic(params).then(res => {
            if (res.code == 200) {
              /*--------删除成功之后获取上一页的数据,避免是否为空--------*/

              //此时已经实现删除操作，所以total的值需要减size，Math.ceil是向上取整，保证始终大于等于0
              const totalPage = Math.ceil((this.total - 1) / 10);

              console.log("totalPage", totalPage);

              //将当前页码与删除数据之后的总页数进行比较，避免当前页码不存在
              const pagenum =
                this.current > totalPage ? totalPage : this.current;

              console.log("获取总条数", pagenum);

              //避免pagenum变为0
              this.current = pagenum < 1 ? 1 : pagenum;
              // 提示弹窗
              this.$message.success("请求成功!");
              this.topiclist();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    // 查询
    searchlook() {
      this.current = 1;
      this.topiclist();
    },
    // 重置
    Reset() {
      this.selectCode = "";
      this.status = "";
      this.topiclist();
    },
    // 分页数据
    handleCurrentChange(val) {
      this.current = val;
      this.topiclist();
    },
    //话题分类
    classify() {
      this.$router.push("/home/gambit");
    },
    // 新增弹窗
    addBialogVisible() {
      this.title = 1;
      this.dialogVisible = true;
      this.typeShow = false;

      this.$refs.mychild.newAddclick("新增数据");
    },
    // 弹窗关闭
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    //添加礼品弹窗
    setUp() {
      console.log("添加礼品");
    }
  }
};
</script>

<style lang="scss" scoped>
.merchantData {
  display: flex;
  align-items: center;
  padding-top: 14px;
  .addmerchant {
    color: #409eff;
    font-size: 18px;
    font-family: Microsoft YaHei;
    font-weight: 400;
  }
  .merchantNum {
    color: #f23a3a;
    font-size: 18px;
    font-family: Microsoft YaHei;
    font-weight: 800;
  }
}

.clearfix {
  padding-bottom: 20px;
}
.bg {
  width: 100%;
  height: 100%;
  background-color: #f4f7fd;
  padding-top: 10px;
}
.box-card {
  width: 98%;
  height: 80px;
  margin: 0 auto;
}
.card {
  width: 98%;
  margin: 0 auto;
  margin-top: 10px;
}
</style>